﻿using SachOnlineLibrary.Public;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace SachOnlineLibrary.DAL
{
    class LienHeDAL
    {
        KetNoiDB db;
        public LienHeDAL()
        {
            db = new KetNoiDB();
        }
        internal bool TaoLienHe(LienHe lh)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = @"insert into LienHe values (@HoTen,@Email,@SDT,@NoiDung,1)";
            cmd.Parameters.AddWithValue("@HoTen", lh.Ten);
            cmd.Parameters.AddWithValue("@Email", lh.Ten);
            cmd.Parameters.AddWithValue("@SDT", lh.Ten);
            cmd.Parameters.AddWithValue("@NoiDung", lh.Ten);
                     
            return db.ExecuteNonQuery(cmd);
        }
        internal DataTable LietKeTatCaLienHe()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from LienHe order by TrangThai";
            return db.ExecuteQuery(cmd);
        }
        internal DataTable LietKeTheoLienHeMa(int ma)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from LienHe where id = @id";
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = ma;
            DataTable dt = new DataTable();
            return dt = db.ExecuteQuery(cmd);
        }
        public bool XoaLienHe(int ma)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "delete from LienHe WHERE id = @ID";
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ma;         
            return db.ExecuteNonQuery(cmd);
        }

        public bool KichHoatTrangThai(int ma, bool trangthai)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "update LienHe set TrangThai = @TrangThai where id = @ID";
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ma;
            cmd.Parameters.Add("@TrangThai", SqlDbType.Bit).Value = trangthai;
            return db.ExecuteNonQuery(cmd);
        }
    }
   
}
